<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :colon="false" :label-col="{ style: { width: '70px' } }">
        <a-form-item label="执行日期" class="w-72" name="dateRange">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="pyjm" class="w-72 ml-2">
          <a-input v-model:value="formState.pyjm" placeholder="员工编号/员工姓名/助记码" />
        </a-form-item>
        <a-form-item name="pyjm2" class="w-72 ml-2">
          <a-input v-model:value="formState.pyjm2" placeholder="处置项目名称/助记码" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="flex justify-end mb-3">
        <BaseExport type="住院" name="工作量统计" code="001136" :search-params="formState">导出</BaseExport>
      </div>
      <BaseTable :loading="loading" column-code="001136" :extra-height="50" fill :data-source="tableData" bordered :scroll="{ x: 1600 }" :pagination="null">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'mc'">
            <MedicineInfo :value="{ mc: record.mc, gg: record.gg, cj: record.cj }" />
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onClickDetail(record)"> 明细 </a-button>
          </template>
        </template>
        <template #summary>
          <a-table-summary fixed>
            <a-table-summary-row>
              <template v-for="column in columnsData" :key="column.dataIndex">
                <a-table-summary-cell v-if="column.dataIndex === 'xh'"> 合计 </a-table-summary-cell>
                <a-table-summary-cell v-else-if="!column.isHide">
                  <div :class="bottomTotal[column.dataIndex] ? 'text-right' : 'text-center'">{{ bottomTotal[column.dataIndex] || '-' }}</div>
                </a-table-summary-cell>
              </template>
            </a-table-summary-row>
          </a-table-summary>
        </template>
      </BaseTable>
    </a-card>
    <Detail v-model:open="open" :row-data="rowData" />
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import { formatAmount } from '@/utils/format.js'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import Detail from './Detail'

const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})
const open = ref(false)
const rowData = ref({})
const onClickDetail = (record) => {
  rowData.value = { ...record, stdate: formState.value.dateRange?.[0], eddate: formState.value.dateRange?.[1] }
  open.value = true
}
const bottomTotal = ref({})
const { loading, tableData, getData } = useTable({
  pagination: false,
  url: '/inp/zyhs/getWorkloadStatisticsRetrievalList',
  params: formState.value,
  immediate: true,
  formatResult: (result) => {
    if (result && result.length > 0) {
      bottomTotal.value = result.pop()
      bottomTotal.value.mzzxje = formatAmount(bottomTotal.value.mzzxje)
      bottomTotal.value.zyzxje = formatAmount(bottomTotal.value.zyzxje)
      bottomTotal.value.allzxje = formatAmount(bottomTotal.value.allzxje)
      bottomTotal.value.zxr = '-'
    }
    return result
  }
})
//获取表头
const columnsData = ref([])
const getTableColumns = async () => {
  const { code, data } = await post('/common/util/getTableMappingCloumnList', { id: '001136' })
  if (code !== 0) return message.error(code)
  columnsData.value = data
}

onMounted(() => {
  getTableColumns()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
